package java_thinking.unit_15._15_5;

import java_thinking.unit_15._15_3.Generator;
import java_thinking.unit_15._15_4.Generators;

import java.util.*;

/**
 * @Description :   创建一个Cocean中BigFish吃LittleFish的例子
 * @author: cww
 * @DateTime: 2019-04-02 23:31
 */
class LittleFish {
    private static int count = 1;
    private  final int id = count++;
    private LittleFish() {}
    @Override
    public String toString() { return "LittleFish " + id; }

    /**
     * 方法返回一个匿名内部类
     * @return
     */
    public static Generator<LittleFish> generator() {
        return new Generator<LittleFish>() {
            @Override
            public LittleFish next() {
                return new LittleFish();
            }
        };
    }
}

class BigFish {
    private static int count = 1;
    private final int id = count++;
    private BigFish() {}
    @Override
    public String toString() { return "BigFish " + id; }

    /**
     * 匿名内部类，直接返回接口对象引用
     */
    public static Generator<BigFish> generator = new Generator<BigFish>() {
        @Override
        public BigFish next() {
            return new BigFish();
        }
    };
}


public class E18_OceanLife {
    public static void eat(LittleFish littleFish , BigFish bigFish){
        System.out.println(bigFish+" eat "+littleFish);
    }

    public static void main(String[] args) {
        Random random = new Random(47);
        Queue<LittleFish> littleFish = new LinkedList<>();
        Generators.fill(littleFish,LittleFish.generator(),15);
        List<BigFish> bigFishSet = new ArrayList<>();
        Generators.fill(bigFishSet,BigFish.generator,4);
        for (LittleFish littleFish1 :littleFish){
            eat(littleFish1,bigFishSet.get(random.nextInt(bigFishSet.size())));
        }

    }
}
